<?php

namespace app\common\cachemodel\chakra;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class ChakraTestRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.chakra_test_record';

    protected $cacheModel = 'chakra.test_record';

    public function SaveData(array $data)
    {
        try
        {
            $id = $this->insertGetId($data);
            $data['id'] = (int)$id;
            CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('last_test_data', $data['uid'])->setCacheTime(10)->setCache($data, 'last_test_data');

            $this->listDataAdd($data['uid'], [$id => $id]);
            return (int)$id;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return 0;
        }
    }

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->whereIn('id', $ids)->column('*', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $index => $itemData)
        {
            $resData[$index] = $this->dataInit($itemData);
        }

        return $resData;
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        $data = arrayKeyData2int($data, ['id', 'uid', 'create_time']);

        if (isset($data['answer_data']))
        {
            $data['answer_data'] = $data['answer_data'] ? json_decode($data['answer_data'], true) : [];
        }

        return $data;
    }

    /**
     * 获取 最后一次 测试信息
     * @param int $uid
     * @return array
     * @throws \Exception
     */
    public function getLastTestData(int $uid)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('last_test_data', $uid)
            ->getDataAndSet(function () use ($uid)
            {
                try
                {
                    return $this->where('uid', $uid)->order('id desc')->findOrFail()->toArray();
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, [], 'last_test_data');

        $resData = $data ? $this->dataInit($data) : [];

        return $resData;
    }

    public function getListIds(int $uid, int $page, int $pageSize = 15)
    {
        $resData = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $uid)
            ->setPage($page)
            ->setPageSize($pageSize)
            ->setSort('desc')
            ->getListPageDataIds(function ($isCache = true, $sortType = 'desc') use ($uid)
            {
                try
                {
                    $data = $this->where(['uid' => $uid])->order("id {$sortType}")->column('id');

                    $resData = [];
                    foreach ($data as $id)
                    {
                        $resData[$id] = ['id' => $id];
                    }

                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'id', 0);

        return $resData;
    }
}